package com.hoe.tsb.fraft.client.role;

import com.hoe.tsb.Configuration;
import com.hoe.tsb.fraft.client.ClientRoleBridge;
import com.hoe.tsb.fraft.enums.RoleType;
import com.hoe.tsb.fraft.event.msg.FRaftMessage;
import com.hoe.tsb.fraft.event.msg.Vote;

/**
 * <p>
 * 拥护者角色
 * </p>
 *
 * @author hoe
 * @version 1.0
 * @date 2022/12/5 16:21
 */
public abstract class SupportRole extends AbstractRole {

    public SupportRole(ClientRoleBridge bridge, Configuration configuration, RoleObserver roleObserver) {
        super(bridge, configuration, roleObserver);
    }

    @Override
    public boolean handleMessage(FRaftMessage msg) {
        if (super.handleMessage(msg)) {
            return true;
        }

        switch (msg.getType()) {
            /*
             * 拉票消息
             */
            case CANVASS:
                //TODO 如果不是“最大可能交付”的策略，则还需要判断发送方的最大日志索引是否超过已提交索引
                mBridge.sendToSpecific(new Vote(), msg.getNodes().get(0).getIp(), msg.getNodes().get(0).getPort());
                return true;
        }
        return false;
    }

    @Override
    protected void doTimeout() {

    }

    @Override
    public void onRoleTurn() {
        //do nothing ...
    }

}
